package kiratest;

import okhttp3.*;

import java.io.IOException;

/**
 * Created by wangning on 2016/6/29.
 */

/*
异步请求实例
 */
public class Asynchronous_Get {
    public static void main(String[] args) {
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   /* public static final MediaType MEDIA_TYPE_MARKDOWN
            = MediaType.parse("multipart/form-data;boundary=BOUNDARY");*/
    public static void run() throws Exception {
        OkHttpClient client = new OkHttpClient();

        /*File file = new File("C:\\Users\\Administrator\\Desktop\\tmp\\me.sql");
        Request request = new Request.Builder()
                .url("http://localhost:8080/upload/1")
                .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
                .build();*/
        Request request = new Request.Builder()
                .url("http://publicobject.com/helloworld.txt")
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
              /*  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                Headers responseHeaders = response.headers();
                for (int i = 0; i < responseHeaders.size(); i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }

                System.out.println(response.body().string());*/
            }

            @Override
             public void onFailure(Request request, Throwable throwable) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                System.out.println("this is kira's test");
                System.out.println(response.body());
                System.out.println("kira's test end");
            }
        });
    }
}
